package com.boc.langxiang.ui.activity.common;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.boc.langxiang.R;
import com.boc.langxiang.contract.common.AddressManageContract;
import com.boc.langxiang.entity.events.AddAddressSuccessEvent;
import com.boc.langxiang.entity.response.AddressEntity;
import com.boc.langxiang.entity.response.AddressListContent;
import com.boc.langxiang.entity.response.BaseResponse;
import com.boc.langxiang.presenter.common.AddressManagePresenter;
import com.boc.langxiang.ui.activity.BaseToolBarActivity;
import com.boc.langxiang.ui.adapter.AddressManageAdapter;
import com.boc.langxiang.util.T;
import com.chad.library.adapter.base.BaseQuickAdapter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.boc.langxiang.R.id.item;

/**
 * 地址管理
 */
public class AddressManageActivity extends BaseToolBarActivity implements AddressManageContract.View {

    @BindView(R.id.recylerview)
    RecyclerView mRecylerview;
    @BindView(R.id.add)
    RelativeLayout mAdd;

    private AddressManageAdapter mAdapter;
    private AddressManageContract.Presenter mPresenter;
    private boolean mNeedRefresh;
    private boolean mSelectAddress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address_manage);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        setToolBarTitle("地址管理");

        Intent intent = getIntent();
        mSelectAddress = intent.getBooleanExtra("selectAddress", false);

        mPresenter = new AddressManagePresenter(this);

        mRecylerview.setHasFixedSize(true);
        mRecylerview.setLayoutManager(new LinearLayoutManager(mContext));

        initAdapter();
        mPresenter.getAddressList();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mNeedRefresh) {
            mNeedRefresh = false;
            mPresenter.getAddressList();
        }
    }

    private void initAdapter() {
        mAdapter = new AddressManageAdapter(mContext, null, mPresenter, this);
        View emptyView = LayoutInflater.from(mContext).inflate(R.layout.view_empty, (ViewGroup) mRecylerview.getParent(), false);
        mAdapter.setEmptyView(emptyView);
        mAdapter.openLoadAnimation();
        mAdapter.isFirstOnly(false);
//        mDemoAdapter.setOnLoadMoreListener(this);
//        mAdapter.openLoadMore(Constant.PAGE_SIZE, true);
        mRecylerview.setAdapter(mAdapter);
        mAdapter.setOnRecyclerViewItemClickListener(new BaseQuickAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                if (mSelectAddress) {
                    AddressEntity item = mAdapter.getItem(position);
                    Intent intent = new Intent();
                    intent.putExtra("data", item);
                    setResult(RESULT_OK, intent);
                    finish();
                }

            }
        });
    }

    @Override
    protected boolean isSetTypeFace() {
        return true;
    }

    @Override
    public void showLoading() {
        showProgressDialog("正在获取您的收货地址...");
    }

    @Override
    public void hideLoading() {
        hideDialog();
    }

    @Override
    public void onError(String code, String msg) {
        mAdapter.setNewData(new ArrayList<AddressEntity>());
        T.showShort(mContext, msg);
    }

    @Override
    public void onGetAddressSuccess(BaseResponse<AddressListContent> response) {
        if (response == null) {
            return;
        }
        AddressListContent content = response.getContent();
        ArrayList<AddressEntity> addrList = content.getAddrList();
        mAdapter.setNewData(addrList);

    }

    @Override
    public void showDeleteLoading() {
//        showProgressDialog();
    }

    @Override
    public void onDeleteAddressSuccess() {
        T.showShort(mContext, "删除一条记录!");
        mPresenter.getAddressList();
    }

    @Override
    public void setDefaultAddr(int id) {
        mPresenter.setDefaultAddress(id);
    }

    @Override
    public void showSetDefaultLoading() {
//        showProgressDialog("设置默认地址...");
    }

    @Override
    public void setDefaultAddressSuccess() {
        Toast.makeText(mContext, "设置成功", Toast.LENGTH_SHORT).show();
        mPresenter.getAddressList();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onAddAddressSuccessEvent(AddAddressSuccessEvent event) {
        mNeedRefresh = true;
    }

    @OnClick(R.id.add)
    public void onClick() {
        startActivity(new Intent(mContext, AddAddressActivity.class));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
